import { proxy } from "ajax-hook";

// ajax-hook-config.js
export const createAjaxHookConfig = (customHandlers: any = {}) => {
  proxy({
    //请求发起前进入
    onRequest: (config, handler) => {
      // 获取累计数据的接口
      if (config.url.includes("api/anchor/livepc/room_stats")) {
        console.log(config, "config.url");
      }

      if (config.url.includes("api/anchor/comment/info")) {
      }

      // 查询福袋列表接口
      if (config.url.includes("api/buyin/marketing/draw/search")) {
      }

      handler.next(config);
    },
    //请求发生错误时进入，比如超时；注意，不包括http状态码错误，如404仍然会认为请求成功
    onError: (err, handler) => {
      console.log(err.type);
      handler.next(err);
    },
    //请求成功后进入
    onResponse: (response, handler) => {
      // 执行自定义的响应处理器
      if (customHandlers.onResponse) {
        customHandlers.onResponse(response, handler);
      } else {
        handler.next(response);
      }
    },
  });
};
